/*
 * Created on Jul 5, 2004
 *
 */
package littlecyberwar.ui;

import javax.swing.*;
import java.util.*;
import littlecyberwar.util.*;
import littlecyberwar.tool.*;
import littlecyberwar.model.*;
import java.awt.event.*;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class RemoveToTrayDialog extends JDialog {
	private static RemoveToTrayDialog instance = new RemoveToTrayDialog();
	
	private static ButtonGroup group;
	private JPanel selections;
	private JPanel overallPanel;
	private int numOfAttrib;
	private HashMap lookup;	// trayname to subtray combox lookup
	InternalCmdFacade mod;

	private RemoveToTrayDialog() {
		selections = new JPanel();
		overallPanel = new JPanel();
		overallPanel.setLayout(new BoxLayout(overallPanel, BoxLayout.PAGE_AXIS));

		selections.setLayout(new SpringLayout());
		group = new ButtonGroup();
		lookup = new HashMap();
		numOfAttrib = 0;
		this.setTitle("Select Tray for Unit Removal");
	}
	
	public void addEntry( String trayName, ArrayList subTrayNames ){
		JRadioButton radioBut = new JRadioButton(trayName);
		radioBut.setActionCommand(trayName);
		group.add( radioBut );
		JComboBox subTrayList = new JComboBox(subTrayNames.toArray());
		subTrayList.setSelectedIndex(0);
		selections.add(radioBut);
		selections.add(subTrayList);
		lookup.put(trayName, subTrayList);
		numOfAttrib ++;
		return;		
	}
	
	public static RemoveToTrayDialog getInstance() {
		return ( instance );
	}
	
	/**
	 * Create the dialog from all the entries added 
	 *
	 */
	public void compose(InternalCmdFacade in ) {
		SpringUtilities.makeCompactGrid(selections, //parent
										numOfAttrib, 2,
										5, 5,  //initX, initY
										4, 4); //xPad, yPad
		overallPanel.add(selections);
		JButton okBut = new JButton("OK");
		overallPanel.add(okBut);
		
		this.getContentPane().add(overallPanel);
		this.pack();
		this.validate();					

		mod = in;
		
		okBut.addActionListener(new ActionListener() {
		   public void actionPerformed(ActionEvent e) {
		   	// first get the choices from the dialog
		   	String trayName = getSelectedTrayName();
		   	if ( trayName != null ){
			   	String subTrayName = getSelectedSubTrayName(trayName);
				PlayingBoardPanel pbp = PlayingBoardPanel.getInstance();
				MouseHandler h = (MouseHandler)pbp.getCurrHandler();
				if ( h instanceof MultiSelectMouseHandler ){
					MultiSelectMouseHandler mh = (MultiSelectMouseHandler)pbp.getCurrHandler();
					ArrayList listOfElements = mh.getSelectedElements(); 
				
					// generate a remove command
					RemoveCommand cmd = new RemoveCommand(trayName, subTrayName, listOfElements);
					 cmd.setPlayerName(Players.getSelfName());
					 mod.addCmd(cmd);
					 // outLabel.setText(sb.toString());
				}
		   	}
			hide();
		   }
		 });

	}
	
	public String getSelectedTrayName() {
		ButtonModel butMod = group.getSelection();
		if ( butMod != null ){
			String action = butMod.getActionCommand();
			// Debug.out("get SelectedTrayName from button press = " + action);
			return ( action );
		} else {
			return ( null );
		}
	}
	
	public String getSelectedSubTrayName( String trayName ){
		JComboBox subTrayList = (JComboBox)lookup.get(trayName);
		String subTrayName = (String)subTrayList.getSelectedItem();
		return ( subTrayName );
	}
	

}
